package com.project_doctor.mapper;

import com.project_doctor.pojo.Conversation;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.project_doctor.pojo.SystemAnnouncement;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
/**
* @author 龙龙与贝儿
* @description 针对表【t_conversation】的数据库操作Mapper
* @createDate 2025-06-07 11:22:38
* @Entity com.project_doctor.pojo.Conversation
*/
public interface ConversationMapper extends BaseMapper<Conversation> {


    @Select("SELECT " +
            "c.*, " +
            "d.id as doctor_id, d.name as doctor_name, d.avatar as doctor_avatar, " +
            "m.id as message_id, m.sender_id as message_sender_id, " +
            "m.is_read as message_is_read, m.conversation_id as message_conversation_id " +
            "FROM t_conversation c " +
            "LEFT JOIN t_doctor d ON c.participant_id = d.id " +
            "LEFT JOIN t_message m ON c.latest_message_id = m.id " +
            "WHERE c.participant_id = #{userId} OR c.starter_id = #{userId}")
    @Results({
            // 显式映射participantId
            @Result(property = "participantId", column = "participant_id"),
            // 显式映射starterId
            @Result(property = "starterId", column = "starter_id"),
            // 修正：直接映射participant对象，而不是其id属性
            @Result(
                    property = "participant",
                    column = "participant_id",
                    one = @One(select = "com.project_doctor.mapper.DoctorMapper.selectById")
            ),

            // 修正：同理映射starter对象
            @Result(
                    property = "starter",
                    column = "starter_id",
                    one = @One(select = "com.project_doctor.mapper.DoctorMapper.selectById")
            ),
            // 修正：同理映射message对象
            @Result(
                    property = "latestMessage",
                    column = "latest_message_id",
                    one = @One(select = "com.project_doctor.mapper.MessageMapper.selectById")
            )
    })
    List<Conversation> getAllConversationsWithTwoProfile(Long userId);
}




